package com.larkin.utils.controller;

import com.larkin.utils.service.impl.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.configuration.JobRegistry;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * Created by liujin on 2019/5/26.
 *
 * @description TODO
 */
@RestController
@CrossOrigin("*")
@RequestMapping(value = "/user")
public class UserInfoController {

    @Autowired
    private UserService userService;
    @Autowired
    JobRegistry jobRegistry;
    @Autowired
    JobLauncher jobLauncher;
    private Logger logger = LoggerFactory.getLogger(UserInfoController.class);

    @ResponseBody
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    public String getInfo() {
        String info = userService.showInfo();
        logger.info("这里返回:{}", info);
        return info;
    }

    @ResponseBody
    @RequestMapping(value = "/handle", method = RequestMethod.GET)
    public String handle() {

        try {
            Job job = jobRegistry.getJob("messageMigrationJob");
            JobExecution jobExecution = jobLauncher.run(job, createJobParams());
            System.out.println(jobExecution);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return userService.showInfo();
    }

    private static JobParameters createJobParams() {
        //设置job运行的参数
        return new JobParametersBuilder().addDate("bizDate", new Date()).toJobParameters();
    }
}
